package com.liruo.learn.spring.mvc.controller;

import java.io.IOException;
import java.net.URL;
import lombok.Builder;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author:liruo
 * @Date:2023-07-22-22:21:15
 * @Desc
 */
@CrossOrigin(origins = "*")
@RequestMapping("/LongDataTest")
@Controller
public class LongDataTestController {

  @Builder
  @Data
  public static class Post {

    private Integer id;
    private Integer authorId;
    private User author;
    private String content;
  }

  @Builder
  @Data
  public static class User {

    private Integer id;
    private String username;
  }

  @ResponseBody
  @GetMapping("/long")
  public Post longPost() throws IOException {
    URL url = new URL("https://juejin.cn/post/7123529478558613512");
    String body = new String(url.openStream().readAllBytes());
    return Post.builder()
        .id(1)
        .authorId(2)
        .author(User.builder()
            .id(2)
            .username("test")
            .build())
        .content(body)
        .build();
  }
  @ResponseBody
  @GetMapping("/short")
  public Post shortPost() throws IOException {
    return Post.builder()
        .id(1)
        .authorId(2)
        .author(User.builder()
            .id(2)
            .username("test")
            .build())
        .content("short===\n".repeat(10))
        .build();
  }
}
